package com.dong.springbootweb.controller;

import com.dong.springbootweb.service.FileUploadService;
import com.dong.springbootweb.util.POIWordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/hello")
//@RestController
@Controller
public class HelloController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * name:姓名
     */
     private String name;
     
    @ResponseBody
    @GetMapping("/hello")
    public String Hello() {
        return "hello";
    }

    @GetMapping("/success")
    public String success(Map<String,Object> maps) {
        maps.put("hello","你好");
        return "success";
    }

    public String error() {
        System.out.println();

        return "error";
    }

    /**
     * 加载经过POI初始化数据过后的url地址
     * @param templateUrl
     * @return
     */
    @ResponseBody
    @GetMapping("/loadInit")
    public String loadInit(@RequestParam(name = "templateUrl") String templateUrl){
        String loadUrl = "";
        try {
            //确定写出文件的位置
            String tempPath = null;
            Map<String, String> parametersMap = new HashMap<String, String>();
            parametersMap.put("courtName","广东省广州市南沙区初级人民法院");
            parametersMap.put("bankruptcyCaseNumber","(2023)穗0128破之一");
            //生成当前时间
            Date d = new Date();
            parametersMap.put("announcementTime",new SimpleDateFormat("yyyy年MM月dd日").format(d));
            parametersMap.put("custodian","xxx股份有限公司");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateNowStr = sdf.format(d);
//            tempPath = System.getProperty("java.io.tmpdir")+ File.separator;
            tempPath = "/data/mydata/onlyoffice"+ File.separator;
            //新生产的模板地址
            String filename = "测试文档"+ dateNowStr;
            System.out.println("执行模板复制、替换 ${关键字}、生成word到指定目录下");
            String outputUrlZIP =  tempPath+File.separator+filename+".docx";
            FileOutputStream fos = new FileOutputStream(outputUrlZIP);
            fos.write(getUrlFileData(templateUrl));
            //用FileOutputStream 的write方法写入字节数组
            System.out.println("写入成功");
            //为了节省IO流的开销，需要关闭
            fos.close();
            loadUrl = POIWordUtil.wordTextSubstitution(outputUrlZIP, "DOCX", parametersMap);
            System.out.println("临时文件路径："+loadUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileUploadService.upload(new File(loadUrl));
    }

    //获取链接地址文件的byte数据
    public static byte[] getUrlFileData(String fileUrl) throws Exception
    {
        URL url = new URL(fileUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.connect();
        InputStream cin = httpConn.getInputStream();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = cin.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        cin.close();
        byte[] fileData = outStream.toByteArray();
        outStream.close();
        return fileData;
    }
}
